#include "ubt_state/ubt_state_task_map.h"
#include "ubt_state/ubt_state_api.h"
#include "ubt_state/ubt_state_util.h"
#include "ubt_state/node.h"

namespace ubt_state
{

ITask *CTaskMap::CreateTask(ITaskCb *pTaskCb, std::string taskId, void *pvReq)
{
    CTaskMap *pTask = new CTaskMap(pTaskCb, taskId, pvReq);
    return (ITask *)pTask;
}

CTaskMap::CTaskMap(ITaskCb *pTaskCb, std::string taskId, void *pvReq)
{
    _pTaskCb = pTaskCb;
    _taskId = taskId;
    _state = ubt_task_state_idle;
    _stopReason = ubt_task_result_ok;
    _obstacleState = 0;
}

CTaskMap::~CTaskMap()
{
}

bool CTaskMap::Start()
{
    if (_state != ubt_task_state_idle)
    {
        return false;
    }
    _state = ubt_task_state_running;
    StateStatus *pStatus = _pTaskCb->GetStateStatus();
    if (pStatus->avoidHwStatus == 1)
    {
        _obstacleState = 1;
    }
    else
    {
        _obstacleState = 0;
    }
    return true;
}

void CTaskMap::Pause()
{
    if (_state == ubt_task_state_running)
    {
        _state = ubt_task_state_pause;
    }
}

void CTaskMap::Continue()
{
    if (_state == ubt_task_state_pause)
    {
        _state = ubt_task_state_running;
    }
}

void CTaskMap::Stop(int reason)
{
    if (_state >= ubt_task_state_stopping)
    {
        return;
    }
    _state = ubt_task_state_closed;
    _stopReason = reason;
    _stopReasonDesc = UbtStateGetTaskResultDesc(_stopReason);
    // _pTaskCb->NotifyTaskEvent(UBT_TASK_EVENT_FINISH_STR);
    _pTaskCb->NotifyTaskStateAndStage(ubt_task_state_closed, ubt_task_stage_mapping, _stopReason, _stopReasonDesc);
    return;
}


void CTaskMap::OnTimer()
{
}

void CTaskMap::OnMsg(int msgType, void *pvMsg)
{
    if (msgType == ubt_msg_type_avoid_status)
    {
        StateStatus *pStatus = _pTaskCb->GetStateStatus();
        int avoidStatus = 0;
        if (pStatus->avoidHwStatus == 1)
        {
            avoidStatus = 1;
        }
        if (_obstacleState != avoidStatus)
        {
            _obstacleState = avoidStatus;
            _pTaskCb->NotifyTaskStateAndStage(GetState(), GetStage(), 0, "");
        }
    }
}

ubt_task_type CTaskMap::GetType()
{
    return ubt_task_type_map;
}

ubt_task_state CTaskMap::GetState()
{
    ubt_task_state state = _state;
    if (state == ubt_task_state_running)
    {
        if (_obstacleState == 1)
        {
            state = ubt_task_state_obstacle;
        }
    }
    return state;

}

ubt_task_stage CTaskMap::GetStage()
{
    return ubt_task_stage_mapping;
}

std::string CTaskMap::GetActionType()
{
    return "map";
}

void CTaskMap::GetIndex(int& index, int& reportIndex)
{
    index = 0;
    reportIndex = 0;
}

void CTaskMap::GetStopInfo(int& stopReason, std::string& stopReasonDesc)
{
    stopReason = _stopReason;
    stopReasonDesc = _stopReasonDesc;
}

void CTaskMap::GetLogs(std::vector<TaskLogItem>& logs)
{
}



}


